(function() {
    var ipt = document.querySelector('input');
    var searchParams = new URL(document.location.href).searchParams
    var filter = document.querySelectorAll('.search-filter')
    var sort = searchParams.get('sort') || '', type = searchParams.get('type') || ''
    ipt.addEventListener('keyup', function(e) {
        if(e.keyCode === 13) {
            if(ipt.value.trim().length > 0) {
                document.location = '/search?key=' + encodeURIComponent(ipt.value) + '&sort=' + sort + '&type=' + type
            }
        }
    });
    document.querySelector('button').addEventListener('click', function(e) {
        if(ipt.value.trim().length > 0) {
            document.location = '/search?key=' + encodeURIComponent(ipt.value) + '&sort=' + sort + '&type=' + type
        }
    });
    filter.forEach(o => {
        o.addEventListener('change', () => {
            if(o.checked) {
                if(o.name === 'type') type = o.value
                if(o.name === 'sort') sort = o.value
                document.location.href = '/search?key=' + encodeURIComponent(ipt.value) + '&sort=' + sort + '&type=' + type
            }
        })
    })
})();